package com.finance.action.admin.finance;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.vo.PageVo;
import com.finance.entity.Finance;
import com.finance.entity.User;
import com.finance.service.FinanceService;
import com.web.utils.DateUtil;
import com.web.utils.UserManagerUtil;

@Controller("FinanceAction")
@RequestMapping("admin/finance")
public class FinanceAction {

	@Resource(name = "FinanceService")
	private FinanceService financeService;

	@RequestMapping("finance_list")
	public String finance_liat() {
		User user = UserManagerUtil.getCurrentUser();
		if (user.getJurisdiction() == 3) {
			return "redirect:role.htm";
		}
		return "manage/finance/finance_list";
	}

	@RequestMapping("role.htm")
	public String role() {
		return "manage/role";
	}

	@ResponseBody
	@RequestMapping("ajax_finance_list")
	public Map<String, Object> ajax_finance_list(
			@RequestParam(defaultValue = "1") int pageNum, Finance search,
			String o, String n) throws UnsupportedEncodingException {
		Map<String, Object> map = new HashMap<String, Object>();

		search.setOldTime(DateUtil.toDate(o));
		search.setNewTime(DateUtil.toDate(n));

		// *********************************UTF-8乱码处理****************************************************//
		if (StringUtils.isNotEmpty(search.getUserName())) {
			String userName = new String(search.getUserName().getBytes(
					"iso-8859-1"), "UTF-8");
			search.setUserName(userName);
		}

		PageVo<Finance> page = financeService.findBySearch(search, pageNum);
		for (int i = 0; i < page.getList().size(); i++) {
			page.getList()
					.get(i)
					.setStart_time(
							DateUtil.Date2Stirng(page.getList().get(i)
									.getStartTime()));
			page.getList()
					.get(i)
					.setStop_time(
							DateUtil.Date2Stirng(page.getList().get(i)
									.getStopTime()));
		}
		// 坏账率
		double a = 0;
		double b = 0;

		double stillMoney = 0;// 实际还款金额
		double homeVisitsMoney = 0;// 家访金额
		double intermediary = 0;// 中介金额
		double bond = 0;// 保证金
		double actualMoney = 0;// 到手金额
		for (int i = 0; i < page.getList().size(); i++) {
			// 总坏账盈利
			a = page.getList().get(i).getLoanMoney() + a;
			b = page.getList().get(i).getAlreadyRepaid() + b;

			stillMoney = page.getList().get(i).getStillMoney() + stillMoney;
			homeVisitsMoney = page.getList().get(i).getHomeVisitsMoney()
					+ homeVisitsMoney;
			intermediary = page.getList().get(i).getIntermediary()
					+ intermediary;
			bond = page.getList().get(i).getBond() + bond;
			actualMoney = page.getList().get(i).getActualMoney() + actualMoney;

			// 用户坏账盈利
			page.getList()
					.get(i)
					.setHuaizhang(
							(page.getList().get(i).getLoanMoney() + page
									.getList().get(i).getAlreadyRepaid())
									/ page.getList().get(i).getLoanMoney()
									* 100 + "%");
			page.getList()
					.get(i)
					.setYinli(
							page.getList().get(i).getStillMoney()
									- (page.getList().get(i)
											.getHomeVisitsMoney()
											+ page.getList().get(i)
													.getIntermediary() + page
											.getList().get(i).getActualMoney())
									- page.getList().get(i).getBond());
		}
		String c = (a + b) / a * 100 + "%";
		double d = stillMoney - (homeVisitsMoney + intermediary + actualMoney)
				- bond;

		map.put("code", 0);
		map.put("msg", 0);
		map.put("c", c);
		map.put("d", d);
		map.put("count", page.getCount());
		map.put("data", page.getList());
		return map;
	}
}
